<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="姓名" prop="realName">
        <el-input v-model="queryParams.realName" placeholder="请输入姓名" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input v-model="queryParams.idCard" placeholder="请输入身份证" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <!-- <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['wine:leave-info:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['wine:leave-info:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="用户昵称" align="center" prop="nickname" />
      <el-table-column label="发布用户" align="center" prop="name" />
      <el-table-column label="姓 名" align="center" prop="realName" />
      <el-table-column label="身份证" align="center" prop="idCard" />
      <el-table-column label="性 别" align="center" prop="sex" />
      <el-table-column label="年 龄" align="center" prop="age" />
      <el-table-column label="置 顶" align="center" prop="isTop">
        <template #default="scope">
          <el-switch :value="scope.row.isTop" :active-value="1" :inactive-value="0" active-text="是" inactive-text="否"
            inline-prompt @click="handleStatusChange(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="audit">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.WINE_CHECK" :value="scope.row.audit" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="handleAudit(scope.row.id)" v-hasPermi="['wine:leave-info:query']">
            详情
          </el-button>
          <el-button v-hasPermi="['wine:leave-info:update']" link type="primary" @click="openDetail(scope.row)"
            v-if="scope.row.audit == 0"> 审核 </el-button>

          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['wine:leave-info:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>
  <Dialog v-model="dialogVisible" title="审核" top="50" align-center width="500" @close=resetForm>
    <el-form ref="formRef" :model="formData" :rules="rules">
      <el-form-item label="审核" prop="audit">
        <el-select placeholder="请选择" v-model="formData.audit" class="w-80">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.WINE_CHECK)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="auditClick">
          确定
        </el-button>
      </div>
    </template>
  </Dialog>
  <!-- 表单弹窗：添加/修改 -->
  <!-- <LeaveInfoForm ref="formRef" @success="getList" /> -->

</template>

<script setup lang="ts">
// import download from '@/utils/download'
import { LeaveInfoApi, LeaveInfoVO } from '@/api/wine/leaveinfo'
import { DICT_TYPE, getIntDictOptions } from "@/utils/dict";
import { ComplainInfoApi } from "@/api/wine/complaininfo";

/** 发布离职通告 列表 */
defineOptions({ name: 'LeaveInfo' })

const dialogVisible = ref(false)

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { push } = useRouter() // 路由跳转
const loading = ref(true) // 列表的加载中
const list = ref<LeaveInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数


const formRef = ref() // 表单 Ref

const formData = ref({
  audit: undefined
})

const rules = reactive({
  audit: [{ required: true, message: '请选择审核', trigger: 'change' }],
})

const resetForm = () => {
  formData.value = {
    audit: undefined
  }
  formRef.value?.resetFields()
}
const auditLoading = ref(false)

const auditClick = async () => {
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  auditLoading.value = true
  auditRow.value.audit = formData.value.audit
  try {
    await LeaveInfoApi.updateLeaveInfo(auditRow.value)
    message.success('操作成功')
    getList()
    auditLoading.value = false
    dialogVisible.value = false
  } finally {
    auditLoading.value = false
    dialogVisible.value = false
  }
}

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: undefined,
  infoName: undefined,
  realName: undefined,
  idCard: undefined,
  sex: undefined,
  birthday: undefined,
  age: undefined,
  image: undefined,
  images: undefined,
  description: undefined,
  isTop: undefined,
  sort: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
// const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await LeaveInfoApi.getLeaveInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleAudit = (id: number) => {
  push({ name: 'LeaveinfoDetail', params: { id } })
}

/** 添加/修改操作 */
// const formRef = ref()
// const openForm = (type: string, id?: number) => {
//   formRef.value.open(type, id)
// }

const auditRow = ref()
const openDetail = (row: object) => {
  console.log(row)
  auditRow.value = row
  dialogVisible.value = true
}


/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await LeaveInfoApi.deleteLeaveInfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}
const handleStatusChange = async (row: any) => {
  try {
    let data = row
    // 二次确认
    const text = data.isTop == 1 ? '取消置顶' : '置顶'
    await message.confirm(`确认要${text}"${row.infoName}"吗？`).then(async (res) => {
      console.log("err", res);
      data.isTop = data.isTop == 1 ? 0 : 1
      // 发起修改
      await LeaveInfoApi.updateLeaveInfo(data)
      message.success(text + '成功')
      // 刷新列表
      await getList()
    }).catch((err) => {
      console.log("err", err);
    })
  } catch {
    // 异常时，需要重置回之前的值
    await getList()
  }
}

// /** 导出按钮操作 */
// const handleExport = async () => {
//   try {
//     // 导出的二次确认
//     await message.exportConfirm()
//     // 发起导出
//     exportLoading.value = true
//     const data = await LeaveInfoApi.exportLeaveInfo(queryParams)
//     download.excel(data, '发布离职通告.xls')
//   } catch {
//   } finally {
//     exportLoading.value = false
//   }
// }

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
